package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class SearchFriend extends Utils {
    public static String url = "http://127.0.0.1:8888/login.html";
    public SearchFriend() {
        super(url);
    }

//    先登录
    public void loginSuss1(){
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("26588");
        driver.findElement(By.cssSelector("#submit")).click();
        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session"));
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
//        成功登录
    }

    public void loginSuss2(){
        driver.findElement(By.cssSelector("#username")).sendKeys("测试用户6");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("26588");
        driver.findElement(By.cssSelector("#submit")).click();
        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session"));
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
//        成功登录
    }

//  20.在搜索框输入“测试”，点击搜索按钮
    public void searchFriend() throws IOException {
        loginSuss1();
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.search > input[type=text]")).sendKeys("测试");
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.search > button")).click();
        getScreenShot("searchFriend");
        String text1 = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
        assert text1.equals("查找结果");
        String text2 = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div:nth-child(7) > span")).getText();
        assert text2.equals("测试用户6");
//        driver.quit();
    }

//    21.在“测试用户6”，中间文本框输入“hello”，点击“+”按钮
    public void addFriend(){
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div:nth-child(7) > input[type=text]")).sendKeys("hello");
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div:nth-child(7) > button")).click();
        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
//        driver.quit();
    }

//    22.登录“测试用户6“，验证是否收到添加好友申请
    public void acceptFriend(){
        loginSuss2();
        String text = driver.findElement(By.cssSelector("#session-list > li > h3")).getText();
        assert text.equalsIgnoreCase("zhangsan");
        driver.findElement(By.cssSelector("#session-list > li > div > button:nth-child(1)")).click();
        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
//        driver.quit();
    }

//    23.点击好友标签，检查是否存在好友“zhansgan”
    public void checkFriend(){
        loginSuss2();
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
        String text = driver.findElement(By.cssSelector("#friend-list > li > h4")).getText();
        assert text.equalsIgnoreCase("zhangsan");
        driver.quit();
    }

}
